package com.cslg.webassets.controller;

import com.cslg.webassets.dao.UserRepository;
import com.cslg.webassets.pojo.User;
import com.cslg.webassets.util.EncrypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/webassets/user")
public class UserController {
    @Autowired
    private UserRepository userRepository;
    @RequestMapping(value="/login",method = {RequestMethod.GET, RequestMethod.POST})
    public String UserLogin(User user, ModelMap modelMap) throws Exception {
        String page="index";
        List<User> userList=userRepository.findUserByusername(user.getUsername());
        if(userList.size()==0)//此用户不存在
        {
            modelMap.addAttribute("message","用户名或者密码不正确");
            page="login";
        }else {
            if(!userList.get(0).getPassword().equals(EncrypeUtil.shaEncode(user.getPassword()))) {
                modelMap.addAttribute("message", "用户名或者密码不正确");
                page="login";
            }
        }
        return page;
    }

    /**
     * 测试代码
     * @return
     */
    @RequestMapping(value="/test",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String TestService(){
        return "测试成功";
    }


    @RequestMapping("/thymeleaftest")
    public String thymeleaftest(ModelMap map) {
        map.addAttribute("host", "http://blog.didispace.com");
        return "/user/WebVulnerabilityList";
    }

    @RequestMapping(value="/demo",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String demo() {
        return "demo";
    }

}
